package com.financing.wap.service.impl;


import java.sql.SQLException;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.financing.wap.common.CommonConstantUtil;
import com.financing.wap.common.CommonConstantUtil.CommonStatus;
import com.financing.wap.dao.IDstUserArticleCategoriesDao;
import com.financing.wap.dto.DstUserArticleCategoriesDTO;
import com.financing.wap.service.IDstUserArticleCategoriesService;
import com.financing.wap.util.DTOdateSetUtil;
import com.financing.wap.vo.UserArticleInfoCategoriesVO;
/**
 * dst_user_article_categories  
 * 用户关联分类和喜爱分类 业务访问层实现类
 * @author huqiyun
 *
 */
 
@Service("dstUserArticleCategoriesService")
@Transactional(rollbackFor=Exception.class)
public class DstUserArticleCategoriesServiceImpl implements IDstUserArticleCategoriesService{

	@Autowired
	private IDstUserArticleCategoriesDao dstUserArticleCategoriesDao;

	@Override
	public int insert(DstUserArticleCategoriesDTO dstUserArticleCategories) throws SQLException {
		DTOdateSetUtil.setCreateInfo(dstUserArticleCategories);
		DTOdateSetUtil.setUpdateInfo(dstUserArticleCategories);
		dstUserArticleCategories.setDeleteTag(CommonStatus.DELETE_TAG_NORMAL);
		return dstUserArticleCategoriesDao.insert(dstUserArticleCategories);
	}

	@Override
	public void update(DstUserArticleCategoriesDTO dstUserArticleCategories) throws SQLException {
		DTOdateSetUtil.setUpdateInfo(dstUserArticleCategories);
		dstUserArticleCategoriesDao.update(dstUserArticleCategories);
	}

	@Override
	public void updateForce(DstUserArticleCategoriesDTO dstUserArticleCategories) throws SQLException {
		DTOdateSetUtil.setUpdateInfo(dstUserArticleCategories);
		dstUserArticleCategoriesDao.updateForce(dstUserArticleCategories);
	}

	@Override
	public void delete(Integer id) throws SQLException {
		dstUserArticleCategoriesDao.delete(id);
	}
	
	@Override
	public List<DstUserArticleCategoriesDTO> queryList(DstUserArticleCategoriesDTO dstUserArticleCategories) throws SQLException {
		return dstUserArticleCategoriesDao.queryList(dstUserArticleCategories);
	}

	@Override
	public int queryListCount(DstUserArticleCategoriesDTO dstUserArticleCategories) throws SQLException {
		return dstUserArticleCategoriesDao.queryListCount(dstUserArticleCategories);
	}

	@Override
	public DstUserArticleCategoriesDTO loadById(Integer id) throws SQLException {
		return dstUserArticleCategoriesDao.loadById(id);
	}
 	@Override
	public DstUserArticleCategoriesDTO queryListGetFirst(DstUserArticleCategoriesDTO dstUserArticleCategories)
			throws SQLException {
		 List<DstUserArticleCategoriesDTO> queryList = queryList(dstUserArticleCategories);
		 if(queryList==null||queryList.size()==0){
			 return new DstUserArticleCategoriesDTO();
		 }
		 return queryList.get(0);
	}
 	
 	@Override
	public List<UserArticleInfoCategoriesVO> queryAllCategoriesByOpenId(
			String openid) throws SQLException {
		return dstUserArticleCategoriesDao.queryAllCategoriesByOpenId(openid);
	}
 	
 	/***
 	 * 批量修改用户的分类
 	 */
	@Override
	public void batchUpdate(String openid, String articleTypeIds)
			throws SQLException {
		
		//删除历史关注标签
		DstUserArticleCategoriesDTO dstUserArticleCategories=new DstUserArticleCategoriesDTO();
		dstUserArticleCategories.setDeleteTag(CommonStatus.DELETE_TAG_DELETE);
		dstUserArticleCategories.setUserOpenid(openid);
		update(dstUserArticleCategories);
		//新增当前关注标签
		String[] article = articleTypeIds.split(",");
		for (int i = 0; i < article.length; i++) {
			if(StringUtils.isBlank(article[i]))continue;
			DstUserArticleCategoriesDTO d=new DstUserArticleCategoriesDTO();
			d.setArticleTypeId(Integer.parseInt(article[i]));
			//查询分类
			d.setUserOpenid(openid);
			insert(d);
		}
	}
	
	
}
